
//暴露一个函数 当连接成功时 传入成功函数 失败时调用时调用失败回调
module.exports = function (success,error) {

const mongoose = require('mongoose');

const dbconfig = require('../config/config');

let {DBHOST,DBPORT,DATABASE} = dbconfig;

mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DATABASE}`);

if (error) {
    error = () => {
        console.log("连接失败!!");
    };
}

//建议用once连接数据库 once 只链接一次
mongoose.connection.once('open',() => {
    success();
});
mongoose.connection.on('close',() => {
    console.log('连接关闭');
});
mongoose.connection.on('error',() => {
    error;
});
}